# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def isBalanced(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        def getDepth(root):
            if not root: return 0
            ldepth = getDepth(root.left)
            rdepth = getDepth(root.right)
            if ldepth==-1 or rdepth==-1 or abs(ldepth-rdepth)>1: return -1
            else: return max(ldepth, rdepth)+1
        return getDepth(root)!=-1